var windowName = "userCharge";
var Page;
apiready = function(){

    onWindowShow(function () {
        console.log(windowName + " show")
    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    Page = createVM("#Page",{
        //页面数据
        curTab:0
        ,money:""
        ,conf:[{price:"-"}]
        ,phone:''
        ,cards:[]
        ,codes:[]
        ,payType:100
    },{
        //页面方法
        "init":function () {
            var conf = getLocData("chargeConfig");
            this.conf = conf
            logJson(conf)

            var platCard = getLocData("platCard");
            Page.phone = platCard.phone;
            Page.cards = platCard.cards;
            Page.codes = platCard.codes;
            apiGet("/app/plat/money/card/get",{},function (res,code) {
                logJson(res,"code")
                setLocData("platCard",res.data)
                Page.phone = res.data.phone;
                Page.cards = res.data.cards;
                Page.codes = res.data.codes;
            })

        }
        ,setCur: function (index) {
            this.cur = index
        }
        ,doPay:function () {
            if (!isTrue(this.money)) {
                showToast("请输入充值金额");
                return false;
            }

            apiGet("/app/user/charge",{payType:this.payType,money:this.money},function (res,code) {
                if (code == 200) {
                    if (Page.payType == 100){
                        //微信支付
                        wxPay(res.data,function (ret) {
                            logJson(ret,"PAY_RET")
                        })
                    }
                    if (Page.payType == 200) {
                        //支付宝支付
                        aliPay(res.data,function (ret,err) {
                            logJson(ret,"ali_ret")
                            logJson(err,"ali_err")
                        });
                    }


                    // aliPay(res.data,function (ret,err) {
                    //     logJson(ret,"ali_ret")
                    //     logJson(err,"ali_err")
                    // })
                }
            })
        }
        ,createOrder:function () {
            createOrder(this)
        }

    })
    Page.init()
}


function createOrder(_this) {
    apiLoad("/app/order/create",{id:_this.cur + 1,type:2},function (res) {
        if (res.d == 1) {
            showConfirm(res.message,goback,goback)
            return;
        }
        logJson(res);
        showToast(res.message)
        _this.sn = res.d.sn
        _this.info = res.d.info
        _this.doPay();
    })
}